/**
 * Copyright (c) 2009 Jim Hurne
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package org.tzconverter.menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import org.tzconverter.util.Icons;

/**
 * The main menu bar for the TimeZoneConverter.  The menu is created this way so
 * that it can be dependency-injected into the various views.  If you know of
 * a better way to handle menus within the context of Presenter-first, please let me know!
 *
 *
 * @since Jun 17, 2009
 */

public class MainMenuBar extends JMenuBar implements MenuView
{
  private static final long serialVersionUID = -177582530530371746L;

  // File Menu Items
  private final JMenuItem loadSettingsItem;
  private final JMenuItem saveSettingsItem;
  private final JMenuItem loadDefaultsItem;
  private final JMenuItem exitItem;

  // View Menu Items
  private final JCheckBoxMenuItem alwaysOnTopItem;
  private final JCheckBoxMenuItem translucentItem;
  private final JMenuItem setOpacityLevelItem;

  // Help Menu Items
  private final JMenuItem aboutItem;

  public MainMenuBar(Icons icons)
  {
    // File Menu
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);

    loadSettingsItem = fileMenu.add("Load Settings");
    loadSettingsItem.setIcon(icons.getLoad());
    loadSettingsItem.setMnemonic(KeyEvent.VK_D);
    loadSettingsItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));

    saveSettingsItem = fileMenu.add("Save Settings");
    saveSettingsItem.setIcon(icons.getSave());
    saveSettingsItem.setMnemonic(KeyEvent.VK_S);
    saveSettingsItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));

    loadDefaultsItem = fileMenu.add("Load Default Settings");
    loadDefaultsItem.setIcon(icons.getReload());
    loadDefaultsItem.setMnemonic(KeyEvent.VK_D);

    fileMenu.addSeparator();
    exitItem = fileMenu.add("Exit");
    exitItem.setIcon(icons.getExit());
    exitItem.setMnemonic(KeyEvent.VK_X);
    this.add(fileMenu);

    // View Menu
    JMenu viewMenu = new JMenu("View");
    viewMenu.setMnemonic(KeyEvent.VK_V);

    alwaysOnTopItem = new JCheckBoxMenuItem("Always On Top");
    alwaysOnTopItem.setIcon(icons.getTop());
    alwaysOnTopItem.setMnemonic(KeyEvent.VK_A);
    alwaysOnTopItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
    viewMenu.add(alwaysOnTopItem);

    translucentItem = new JCheckBoxMenuItem("Translucent");
    translucentItem.setIcon(icons.getTranslucent());
    translucentItem.setMnemonic(KeyEvent.VK_T);
    translucentItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
    viewMenu.add(translucentItem);

    viewMenu.addSeparator();
    setOpacityLevelItem = viewMenu.add("Set Opacity Level");
    setOpacityLevelItem.setIcon(icons.getSettings());
    setOpacityLevelItem.setMnemonic(KeyEvent.VK_L);

    this.add(viewMenu);

    // Help Menu
    JMenu helpMenu = new JMenu("Help");
    helpMenu.setMnemonic(KeyEvent.VK_H);
    aboutItem = helpMenu.add("About");
    aboutItem.setIcon(icons.getHelp());
    aboutItem.setMnemonic(KeyEvent.VK_A);
    aboutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));

    this.add(helpMenu);
  }

  /**
   * Gets the JMenuItem for the given item.  If no item is found, null is returned.
   * Passing in null is a programming error.
   *
   * @param menuItem
   * @return
   */
  private JMenuItem getItem(Item menuItem)
  {
    switch(menuItem)
    {
      //File Menu Items
      case LOAD_SETTINGS:
        return loadSettingsItem;
      case SAVE_SETTINGS:
        return saveSettingsItem;
      case LOAD_DEFAULTS:
        return loadDefaultsItem;
      case EXIT:
        return exitItem;

      //View Menu Items
      case ALWAYS_ON_TOP:
        return alwaysOnTopItem;
      case TRANSLUCENT:
        return translucentItem;
      case OPACITY_LEVEL:
        return setOpacityLevelItem;

      //Help Menu Items
      case ABOUT:
        return aboutItem;
    }
    return null;
  }


  @Override
  public void setEnabled(Item menuItem, boolean enabled)
  {
    getItem(menuItem).setEnabled(enabled);
  }

  @Override
  public void setState(Item menuItem, boolean checkBoxMenuState)
  {
    ((JCheckBoxMenuItem) getItem(menuItem)).setState(checkBoxMenuState);
  }

  @Override
  public boolean getState(Item menuItem)
  {
    return ((JCheckBoxMenuItem) getItem(menuItem)).getState();
  }

  @Override
  public void addActionListener(Item menuItem, ActionListener listener)
  {
    getItem(menuItem).addActionListener(listener);
  }

  @Override
  public void addItemListener(Item menuItem, ItemListener listener)
  {
    getItem(menuItem).addItemListener(listener);
  }

  @Override
  public void removeActionListener(Item menuItem, ActionListener listener)
  {
    getItem(menuItem).removeActionListener(listener);
  }

  @Override
  public void removeItemListener(Item menuItem, ItemListener listener)
  {
    getItem(menuItem).removeItemListener(listener);
  }
}
